#!/bin/bash

# Restore script for Hysteria data

BACKUP_DIR="/backup/hysteria"
DATA_DIR="/var/lib/hysteria"
CONFIG_DIR="/etc/hysteria"

echo "Available backups:"
echo ""
echo "Databases:"
ls -lh "$BACKUP_DIR"/*.db 2>/dev/null || echo "No database backups found"
echo ""
echo "Configurations:"
ls -lh "$BACKUP_DIR"/*.tar.gz 2>/dev/null || echo "No config backups found"
echo ""

read -p "Enter database backup filename (or press Enter to skip): " DB_FILE
read -p "Enter config backup filename (or press Enter to skip): " CONFIG_FILE

# Stop services
echo "Stopping services..."
systemctl stop hysteria hysteria-admin

# Restore database
if [ -n "$DB_FILE" ] && [ -f "$BACKUP_DIR/$DB_FILE" ]; then
    echo "Restoring database..."
    cp "$BACKUP_DIR/$DB_FILE" "$DATA_DIR/admin.db"
    chown hysteria:hysteria "$DATA_DIR/admin.db"
fi

# Restore configuration
if [ -n "$CONFIG_FILE" ] && [ -f "$BACKUP_DIR/$CONFIG_FILE" ]; then
    echo "Restoring configuration..."
    tar -xzf "$BACKUP_DIR/$CONFIG_FILE" -C "$CONFIG_DIR"
    chown -R hysteria:hysteria "$CONFIG_DIR"
fi

# Start services
echo "Starting services..."
systemctl start hysteria hysteria-admin

echo "Restore completed!"
